import com.zhangrui.dao.ZhangruiMapper;
import com.zhangrui.pojo.Zhangrui;
import com.zhangrui.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.List;

/**
 * ClassName:Text
 * Package:PACKAGE_NAME
 * Description:
 *
 * @Date:2021/8/3016:15
 * @Author:zhangrui@qq.com
 */
public class Text {
    Logger logger = Logger.getLogger(Text.class);

    //测试一级缓存的测试方法
    @Test
    public void text() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ZhangruiMapper zhangruiMapper = sqlSession.getMapper(ZhangruiMapper.class);
        List<Zhangrui> zhangruiId = zhangruiMapper.getUserId(1);
        logger.info(zhangruiId);
        logger.info("========================================");
//        用于测试进行增删改操作时，缓存会被更新
//        zhangruiMapper.updateUser(new Zhangrui(2, "333333", "666666"));
//        用于测试手动清理缓存后，缓存会被更新
        sqlSession.clearCache();
        List<Zhangrui> zhangruis = zhangruiMapper.getUserId(1);
        logger.info(zhangruis);
        logger.info(zhangruiId == zhangruis);
        sqlSession.commit();
        sqlSession.close();
    }

    //    测试二级缓存的测试方法
    @Test
    public void text2() {
//        创建两个sqlsession会话
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        SqlSession sqlSession1 = MybatisUtils.getSqlSession();
//        获取两个mapper反射实现类（接口的实现文件）
        ZhangruiMapper zhangruiMapper = sqlSession.getMapper(ZhangruiMapper.class);
        ZhangruiMapper zhangruiMapper1 = sqlSession1.getMapper(ZhangruiMapper.class);
//        同时在两个会话中重复查询相同的数据，看是否存在一级缓存，若不存在，再开启二级缓存，看看是否存在二级缓存
        List<Zhangrui> zhangruiList = zhangruiMapper.getUserId(1);
        logger.info(zhangruiList);
//        关闭第一个会话后，上边的查询的一级缓存消失，并把缓存继承给二级缓存
        sqlSession.close();
        logger.info("==========================================");
        List<Zhangrui> mapper1UserId = zhangruiMapper1.getUserId(1);
//        二级缓存生效，不需要再次查询，可以直接从二级缓存中拿数据
        logger.info(mapper1UserId);
//        关闭会话
        sqlSession1.close();
    }
}
